package monaco;

import data.ZProjuectData.ZProjectData;
import monaco.Api.XmlEditorContent;
import haxe.io.Path;

using StringTools;

class CommonentProvideCompletionItems {
	public static var data = FileMacro.bind("./src/monaco/hxmaker-api.xml");

	public static var classes:Map<String, Xml> = [];

	public static var suggestions:Array<Dynamic> = [];

	public static var inited = false;

	public static function init():Void {
		inited = true;
		var xml = Xml.parse(data);
		for (item in xml.firstElement().elements()) {
			classes.set(item.nodeName, item);
		}
	}

	/**
	 * 获得资源路径提示
	 * @param leftInput 
	 * @return SuggestionsData
	 */
	public static function getResources(leftInput:String, spriteUseName:Bool = false):SuggestionsData {
		if (XmlEditorContent.currentProjectData != null) {
			var list = [];
			for (key => png in XmlEditorContent.currentProjectData.pngFiles) {
				var path = png.replace(XmlEditorContent.currentProjectData.rootPath + "/", "");
				if (path.indexOf("Assets") == 0) {
					path = "assets" + path.substring(6);
				}
				var useSpriteUseName = spriteUseName && XmlEditorContent.currentProjectData.sprtesFiles.exists(key);
				list.push(Suggestions.create(key, useSpriteUseName ? key : path, XmlEditorContent.currentProjectData.sprtesFiles.exists(key) ? "精灵图集文件" : "图片文件",
					"Property", leftInput + png));
			}
			return {
				suggestions: list
			}
		}
		return {
			suggestions: []
		}
	}

	/**
	 * 获得XML文件配置提示
	 * @return SuggestionsData
	 */
	public static function getXmlFileConfig(project:ZProjectData, leftInput:String):SuggestionsData {
		var list = [];
		for (item in project.builderFiles) {
			list.push(Suggestions.create(item.name, item.name.replace(".xml", ""), item.path, "Property", leftInput + item.name));
		}
		return {
			suggestions: list
		}
	}

	public static function getClasses(leftInput:String):SuggestionsData {
		if (!inited) {
			CommonentProvideCompletionItems.init();
		}
		var list = [];
		for (key => item in classes) {
			if (key == "Base")
				continue;
			list.push(Suggestions.create(key, key, item.exists("tips") ? item.get("tips") : "TODO", "Property", leftInput + key));
		}
		return {
			suggestions: list
		}
	}

	public static function getProperties(leftInput:String, content:String, className:String):SuggestionsData {
		if (!inited) {
			CommonentProvideCompletionItems.init();
		}
		function readProperties(className:String):Array<Dynamic> {
			var c = classes.get(className);
			if (c == null) {
				c = classes.get("Base");
			}
			if (c != null) {
				var list = [];
				for (item in c.elements()) {
					var key = item.nodeName;
					if (content.indexOf(key + "=") == -1) {
						list.push(Suggestions.create(key, key + '=', item.exists("tips") ? item.get("tips") : "TODO", "Property", leftInput + key));
					}
				}
				if (c.exists("extends")) {
					list = list.concat(readProperties(c.get("extends")));
				}
				return list;
			}
			return [];
		}
		return {
			suggestions: readProperties(className)
		}
	}

	/**
	 * 读取精灵图集中的选项提示
	 * @param leftInput 
	 * @param content 
	 * @param className 
	 * @return SuggestionsData
	 */
	public static function getSpriteIdsProperties(leftInput:String, spriteId:String):SuggestionsData {
		var sprite = XmlEditorContent.currentProjectData.sprtesFiles.get(spriteId);
		var list = [];
		if (sprite != null) {
			var xmlPath = sprite.replace(".png", ".xml");
			var xmlContent = Xml.parse(XmlEditorContent.readFile(xmlPath));
			for (item in xmlContent.firstElement().elements()) {
				list.push(Suggestions.create(item.get("name"), item.get("name"), "精灵图子对象", "Property", leftInput + item.get("name")));
			}
		}
		return {
			suggestions: list
		}
	}
}
